import {
  OrderModel
} from '../../models/orderData.js'
let orderModel = new OrderModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    aplReason: '',
    cancelReason: [],
    reasonIndex: 0,
    fId: '',
  },

  reasonSelector(e) {
    this.setData({
      reasonIndex: e.detail.value
    })
  },

  phoneEvent(e) {
    this.setData({
      phone: e.detail.detail.value
    })
  },

  reasonEvent(e) {
    this.setData({
      reason: e.detail.detail.value
    })
  },

  cancelOrder() {
    orderModel.cancelOrder({
      args: {
        orderId: this.data.fId,
        mobile: this.data.phone,
        cancelCause: this.data.reasonIndex,
        applyCancelRemark: this.data.aplReason
      }
    }).then(res => {
      if (res.data.success) {
        wx.showModal({
          showCancel: false,
          title: '提示',
          content: '取消成功',
          success(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 2
              })
            }
          }
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setNavigationBarTitle({
      title: '取消订单',
    })
    wx.setNavigationBarColor({
      frontColor: '#ffffff',
      backgroundColor: '#3B80FE',
      animation: {
        duration: 400,
        timingFunc: 'easeIn'
      }
    })
    this.setData ({
      fId: options.FId,
      phone: options.phone,
    })
    orderModel.getCancelReasonDic()
    .then(res => {
      let fields = res.data.data.titleJson.fields
      for(var i = 0; i < fields.length; i++) {
        if (fields[i].name === 'FCancelCause') {
          this.setData({
            cancelReason: fields[i].items
          })
        }
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})